Create Table SanPham
(
IDSanPham int identity(1,1),
TenSanPham nvarchar(255),
IDLoaiSP int,
IDDiaDiem int,
IDHangSanXuat int,
GiaBan int,
GiaTriThuc int,
DieuKienSuDung nvarchar(255),
MoTaSP nvarchar(255),
SoLuongSP int,
SoLuongBan int,
SoLuongDaBan int,
IDTrangThaiSP int,
NgayBDBan datetime,
NgayKTBan datetime,
NgayCapNhat datetime,
IDNhanVien int,
IDDoanhNghiep int,
TiLeHoaHong float,
primary key (IDSanPham)
)

Create Table LoaiSP
(
IDLoaiSP int identity(1,1),
TenLoaiSP nvarchar(255),
primary key (IDLoaiSP)
)

Create Table HangSanXuat
(
IDHangSanXuat int identity(1,1),
TenHangSanxuat nvarchar(255),
DiaChi nvarchar(255),
DienThoai int,
Email nvarchar(255),
primary key (IDHangSanXuat)
)

Create Table TrangThaiSP
(
IDTrangThaiSP int identity(1,1),
TrangThai varchar(50),
primary key (IDTrangThaiSP)
)

Create Table DiaDiem
(
IDDiaDiem int identity(1,1),
DiaDiem nvarchar(255),
primary key (IDDiaDiem)
)

Create Table DoanhNghiep
(
IDDoanhNghiep int identity(1,1),
TenDoanhNghiep nvarchar(255),
DiaChi nvarchar(255),
DienThoai int,
Email nvarchar(255),
primary key (IDDoanhNghiep)
)

Create Table NhanVien
(
IDNhanVien int identity(1,1),
TenNV nvarchar(255),
DiaChi nvarchar(255),
DienThoai int,
Email nvarchar(255),
primary key (IDNhanVien)
)

Create Table HinhAnh
(
IDHinhAnh int identity(1,1),
IDSanPham int,
TenHinh nvarchar(255),
primary key (IDHinhAnh)
)

Create Table KhachHang
(
IDKhachHang int identity(1,1),
TenKH nvarchar(255),
DiaChi nvarchar(255),
DienThoai int,
Email nvarchar(255),
primary key (IDKhachHang)
)

Create Table DonHang
(
IDDonHang int identity(1,1),
IDKhachHang int,
NgayDH datetime,
IDTTDonHang int,
TriGia int,
NgayGiaoHang datetime,
primary key (IDDonHang)
)

Create Table ChiTietDH
(
IDCTDH int identity (1,1),
IDSanPham int,
IDDonHang int,
SoLuong int,
ThanhTien int,
primary key (IDCTDH)
)

Create Table TTDonHang
(
IDTTDonHang int identity(1,1),
TrangThaiDH char(10),
primary key (IDTTDonHang)
)

Create Table MaSoSP
(
IDMSSP int identity(1,1),
IDCTDH int,
MaSo int,
TrangThai char(10),
primary key (IDMSSP)
)

Create Table TTGiaoDich
(
IDTTGiaoDich int identity(1,1),
IDDonHang int,
IDHinhThuc int,
ThanhTien int,
primary key(IDTTGiaoDich)
)

Create Table HTThanhToan
(
IDHinhThuc int identity (1,1),
TenHinhThuc nvarchar (50),
primary key (IDHinhThuc)
) 

----xoa san pham------------------------------------------------
CREATE PROCEDURE dbo.SanPham_Delete
	@IDSanPham INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbCTDonHang
WHERE IDSanPham = @IDSanPham
IF @TOTAL = 0
BEGIN
DELETE FROM SanPham where IDSanPham = @IDSanPham
END

-----xoa dia diem-------------------------------------------------
CREATE PROCEDURE dbo.DiaDiem_Delete
	@IDDiaDiem INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDDiaDiem = @IDDiaDiem
IF @TOTAL = 0
BEGIN
DELETE FROM IDDiaDiem where IDDiaDiem= @IDDiaDiem
END
-----xoa doanh nghiep----------------------------------------------
CREATE PROCEDURE dbo.DoanhNghiep_Delete
	@IDDoanhNghiep INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDDoanhNghiep = @IDDoanhNghiep
IF @TOTAL = 0
BEGIN
DELETE FROM IDDoanhNghiep where IDDoanhNghiep= @IDDoanhNghiep
END

-----xoa hang san xuat-----------------------------------------------
CREATE PROCEDURE dbo.HangSanXuat_Delete
	@IDHangSanXuat INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDHangSanXuat = @IDHangSanXuat
IF @TOTAL = 0
BEGIN
DELETE FROM IDHangSanXuat where IDHangSanXuat= @IDHangSanXuat
END
-----xoa hinh anh-----------------------------------------------------
CREATE PROCEDURE dbo.HinhAnh_Delete
	@IDHinhAnh INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDHinhAnh = @IDHinhAnh
IF @TOTAL = 0
BEGIN
DELETE FROM IDHinhAnh where IDHinhAnh= @IDHinhAnh
END

-----xoa bang khach hang-----------------------------------------------------
CREATE PROCEDURE dbo.KhachHang_Delete
	@IDKhachHang INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbDonHang
WHERE IDKhachHang = @IDKhachHang
IF @TOTAL = 0
BEGIN
DELETE FROM IDKhachHang where IDKhachHang= @IDKhachHang
END
-----xoa bang loai san pham---------------------------------------------------
CREATE PROCEDURE dbo.LoaiSP_Delete
	@IDLoaiSP INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDLoaiSP = @IDLoaiSP
IF @TOTAL = 0
BEGIN
DELETE FROM IDLoaiSP where IDLoaiSP= @IDLoaiSP
END

-----xoa bang nhan vien--------------------------------------------------------
CREATE PROCEDURE dbo.NhanVien_Delete
	@IDNhanVien INT 
AS
DECLARE @TOTAL INT
SELECT @TOTAL = COUNT(*) FROM tbSanPham
WHERE IDNhanVien = @IDNhanVien
IF @TOTAL = 0
BEGIN
DELETE FROM IDNhanVien where IDNhanVien= @IDNhanVien
END


------------------cap nhat loai san pham -----------------------
CREATE PROCEDURE dbo.LoaiSP_Update
	@MaLoai int,
	@TenLoai NVARCHAR(255)
AS
UPDATE LoaiSP SET
	TenLoaiSP=@TenLoai 
WHERE IDLoaiSP = @MaLoai

---------------cap nhat hSX--------------------------
CREATE PROCEDURE dbo.HangSanXuat_Update
	@MaHangSX int,
	@TenHangSX NVARCHAR(255),
	@DiaChi NVARCHAR(255),
	@DienThoai int,
	@Email NVARCHAR(255)
AS
UPDATE HangSanXuat SET
	TenHangSanXuat=@TenHangSX ,
	DiaChi=@DiaChi,
	DienThoai=@DienThoai,
	Email=@Email
WHERE IDHangSanXuat = @MaHangSX

-------------cap nhat nhan vien ----------------------------
CREATE PROCEDURE dbo.NhanVien_Update
	@MaNV int,
	@TenNV NVARCHAR(255),
	@DiaChi NVARCHAR(255),
	@DienThoai int,
	@Email NVARCHAR(255)
AS
UPDATE NhanVien SET
	TenNV=@TenNV ,
	DiaChi=@DiaChi,
	DienThoai=@DienThoai,
	Email=@Email
WHERE IDNhanVien = @MaNV
--------------cap nhat doanh nghiep---------------------------
CREATE PROCEDURE dbo.DoanhNghiep_Update
	@MaDN int,
	@TenDN NVARCHAR(255),
	@DiaChi NVARCHAR(255),
	@DienThoai int,
	@Email NVARCHAR(255)
AS
UPDATE DoanhNghiep SET
	TenDoanhNghiep=@TenDN ,
	DiaChi=@DiaChi,
	DienThoai=@DienThoai,
	Email=@Email
WHERE IDDoanhNghiep = @MaDN

------------cap nhat ctdh------------------------------
CREATE PROCEDURE dbo.ChiTietDH_Update
	@IDCTDH int,
	@IDSanPham int,
	@IDDonHang int,
	@SoLuong int,
	@ThanhTien int
AS
UPDATE ChiTietDH SET
	IDSanPham=@IDSanPham,
	IDDonHang=@IDDonHang,
	SoLuong=@SoLuong,
	ThanhTien=@ThanhTien
WHERE IDCTDH = @IDCTDH
------------cap nhat dia diem--------------
CREATE PROCEDURE dbo.DiaDiem_Update
	@IDDiaDiem int,
	@DiaDiem nvarchar(255)
AS
UPDATE DiaDiem SET
	DiaDiem=@DiaDiem
WHERE IDDiaDiem = @IDDiaDiem
-------------cap nhat don hang-------------
CREATE PROCEDURE dbo.DonHang_Update
	@IDDonHang int,
	@IDKhachHang int,
	@NgayDH datetime,
	@IDTTDonHang int,
	@TriGia int,
	@NgayGiaoHang datetime
AS
UPDATE DonHang SET
	IDKhachHang=@IDKhachHang,
	NgayDH=@NgayDH,
	IDTTDonHang=@IDTTDonHang,
	TriGia=@TriGia,
	NgayGiaoHang=@NgayGiaoHang
WHERE IDDonHang=@IDDonHang
-----------cap nhat hinh anh------------
CREATE PROCEDURE dbo.HinhAnh_Update
	@IDHinhAnh int,
	@IDSanPham int,
	@TenHinh nvarchar(255)
AS
UPDATE HinhAnh SET
	IDSanPham=@IDSanPham,
	TenHinh=@TenHinh
WHERE IDHinhAnh = @IDHinhAnh
----------cap nhat HTThanhtoan-------------
CREATE PROCEDURE dbo.HTThanhToan_Update
	@IDHinhThuc int,
	@TenHinhThuc nvarchar(50)
AS
UPDATE HTThanhToan SET
	TenHinhThuc=@TenHinhThuc
WHERE IDHinhThuc = @IDHinhThuc
-----------cap nhat khach hang----
CREATE PROCEDURE dbo.KhachHang_Update
	@IDKhachHang int,
	@TenKH NVARCHAR(255),
	@DiaChi NVARCHAR(255),
	@DienThoai int,
	@Email NVARCHAR(255)
AS
UPDATE KhachHang SET
	TenKH=@TenKH ,
	DiaChi=@DiaChi,
	DienThoai=@DienThoai,
	Email=@Email
WHERE IDKhachHang = @IDKhachHang
------------cap nhat mSSSP---------
CREATE PROCEDURE dbo.MaSoSP_Update
	@IDMSSP int,
	@IDCTDH int,
	@MaSo int,
	@TrangThai char (10)
AS
UPDATE MaSoSP SET
	IDCTDH=@IDCTDH ,
	MaSo=@MaSo,
	TrangThai=@TrangThai
WHERE IDMSSP = @IDMSSP
--------cap nhat san pham ----
CREATE PROCEDURE dbo.SanPham_Update
	@IDSP int,
	@TenSP Nvarchar(250),
	@IDL int,
	@IDDiaDiem int,
	@IDHangSanXuat int,
	@GiaBan int,
	@GiaTriThuc int,
	@DKSuDung Nvarchar(255),
	@MoTaSP Nvarchar(255),
	@SLSanPham int,
	@SLBan int,
	@SLDaBan int,
	@IDTrangThaiSP int,
	@NgayBDBan datetime,
	@NgayKTBan datetime,
	@NgayCapNhat datetime,
	@IDNhanVien int,
	@IDDoanhNghiep int,
	@TiLeHoaHong float
As
	Update SanPham
	Set	TenSanPham = @TenSP,
	IDLoaiSP = @IDL,
	IDDiaDiem =@IDDiaDiem,
	IDHangSanXuat=@IDHangSanXuat,
	GiaBan=@GiaBan,
	GiaTriThuc=@GiaTriThuc,
	DieuKienSuDung=@DKSuDung,
	MoTaSP=@MoTaSP,
	SoLuongSP=@SLSanPham,
	SoLuongBan=@SLBan,
	SoLuongDaBan=@SLDaBan,
	IDTrangThaiSP=@IDTrangThaiSP,
	NgayBDBan=@NgayBDBan,
	NgayKTBan=@NgayKTBan,
	NgayCapNhat=@NgayCapNhat,
	IDNhanVien=@IDNhanVien,
	IDDoanhNghiep=@IDDoanhNghiep,
	TiLeHoaHong=@TiLeHoaHong
	Where IDSanPham = @IDSP
----------cap nhat ttdh-------------
CREATE PROCEDURE dbo.TTDonHang_Update
	@IDTTDonHang int,
	@TrangThaiDH char(10)
AS
UPDATE TTDonHang SET
	TrangThaiDH=@TrangThaiDH
WHERE IDTTDonHang = @IDTTdonHang
----------cap nhat ttsp-------------
CREATE PROCEDURE dbo.TrangThaiSP_Update
	@IDTrangThaiSP int,
	@TrangThai varchar(50)
AS
UPDATE TrangThaiSP SET
	TrangThai=@TrangThai
WHERE IDTrangThaiSP = @IDTrangThaiSP
----------cap nhat ttgd-------------
CREATE PROCEDURE dbo.TTGiaoDich_Update
	@IDTTGiaoDich int,
	@IDDonHang int,
	@IDHinhThuc int,
	@ThanhTien int
AS
UPDATE TTGiaoDich SET
	IDDonHang=@IDDonHang,
	IDHinhThuc=@IDHinhThuc,
	ThanhTien=@ThanhTien
WHERE IDTTGiaoDich = @IDTTGiaoDich


---------------insert ctdh----------------
CREATE PROCEDURE dbo.ChiTietDH_Insert
@IDSanPham int,
@IDDonHang int,
@SoLuong int,
@ThanhTien int
AS
INSERT INTO ChiTietDH VALUES(
@IDSanPham,
@IDDonHang,
@SoLuong,
@ThanhTien 
)

-----------------insert dd---------------
CREATE PROCEDURE dbo.DiaDiem_Insert
@DiaDiem nvarchar(255)
AS
INSERT INTO DiaDiem VALUES(
@DiaDiem
)

-----------------insert dn---------------
CREATE PROCEDURE dbo.DoanhNghiep_Insert
@TenDoanhNghiep nvarchar(255),
@DiaChi nvarchar(255),
@DienThoai int,
@Email nvarchar(255)
AS
INSERT INTO DoanhNghiep VALUES(
@TenDoanhNghiep,
@DiaChi,
@DienThoai,
@Email
)
-----------------insert dh---------------
CREATE PROCEDURE dbo.DonHang_Insert
@IDKhachHang int,
@NgayDH datetime,
@IDTTDonHang int,
@TriGia int,
@NgayGiaoHang datetime
AS
INSERT INTO DonHang VALUES(
@IDKhachHang,
@NgayDH,
@IDTTDonHang,
@TriGia,
@NgayGiaoHang
)

----------------insert ha----------------
create procedure dbo.HinhAnh_insert
@IDSanPham int,
@TenHinh nvarchar(255)
as
insert into HinhAnh values (
@IDSanPham ,
@TenHinh
)

----------------insert htthanhtoan----------------
create procedure dbo.HTThanhToan_insert
@TenHinhThuc nvarchar(50)
as
insert into HTThanhToan values (
@TenHinhThuc
)

----------------insert khach hang----------------
create procedure dbo.KhachHang_insert
@TenKH nvarchar(255),
@DiaChi nvarchar(255),
@DienThoai int,
@Email nvarchar(255)
as
insert into KhachHang values (
@TenKH ,
@DiaChi ,
@DienThoai ,
@Email
)
----------------insert loai sp----------------
create procedure dbo.LoaiSP_insert
@TenLoaiSP nvarchar (255)
as 
insert into LoaiSP values (
@TenLoaiSP
)

----------------insert Massp----------------
create procedure dbo.MaSoSP_insert
@IDCTDH int,
@MaSo int,
@TrangThai char(10)
as 
insert into MaSoSP values (
@IDCTDH,
@MaSo,
@TrangThai
)

----------------insert nhanvien----------------
create procedure dbo.NhanVien_insert
@TenNV nvarchar (255),
@DiaChi nvarchar (255),
@DienThoai int,
@Email nvarchar(255)
as
insert into NhanVien values (
@TenNV ,
@DiaChi ,
@DienThoai ,
@Email
)

----------------insert sanpham----------------
create procedure dbo.SanPham_insert
@TenSanPham nvarchar (255),
@IDLoaiSP int,
@IDDiaDiem int,
@IDHangSanXuat int,
@GiaBan int,
@GiaTriThuc int,
@DieuKienSuDung nvarchar(255),
@MoTaSP nvarchar(255),
@SoLuongSP int,
@SoLuongBan int,
@SoLuongDaBan int,
@IDTrangThaiSP int,
@NgayBDBan datetime,
@NgayKTBan datetime,
@NgayCapNhat datetime,
@IDNhanVien int,
@IDDoanhNghiep int,
@TiLeHoaHong float
as
insert into SanPham values (
@TenSanPham ,
@IDLoaiSP ,
@IDDiaDiem ,
@IDHangSanXuat,
@GiaBan ,
@GiaTriThuc ,
@DieuKienSuDung,
@MoTaSP ,
@SoLuongSP,
@SoLuongBan ,
@SoLuongDaBan,
@IDTrangThaiSP,
@NgayBDBan ,
@NgayKTBan,
@NgayCapNhat,
@IDNhanVien,
@IDDoanhNghiep,
@TiLeHoaHong
)

----------------insert ttsp----------------
create procedure dbo.TrangThaiSP_insert
@TrangThai varchar(50)
as
insert into TrangThaiSP values (
@TrangThai
)
----------------insert ttdh----------------
create procedure dbo.TTDonHang_insert
@TrangThaiDH char(10)
as
insert into TTDonHang values (
@TrangThaiDH
)
----------------insert ttgd----------------
create procedure dbo.TTGiaoDich_insert
@IDDonHang int,
@IDHinhThuc int,
@ThanhTien int
as
insert into TTGiaoDich values (
@IDDonHang,
@IDHinhThuc,
@ThanhTien
)
----------------insert hsx----------------
create procedure dbo.HangSanXuat_insert
@TenHangSanxuat nvarchar (255),
@DiaChi nvarchar (255),
@DienThoai int,
@Email nvarchar(255)
as
insert into HangSanXuat values (
@TenHangSanxuat ,
@DiaChi,
@DienThoai,
@Email
)
----------------phuong thuc get-------------
create Procedure dbo.tbChiTietDH_Get
	@Where NVarchar(255),
	@PageIndex int,
	@PageSize int,
	@OrderBy NVarchar(25),
	@OrderDirection NVarchar(4),
	@TotalRecords int output	
As
	set transaction isolation level read committed
	Create Table #Temp
	(
		IDTemp int identity(1,1),
		IDCTDH int
	)
	Declare @sql NVarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDCTDH]) Select [IDCTDH] From ChiTietDH ' + @Where + ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec (@sql)
	Select @TotalRecords = count (*) from #Temp
	Select ctdh.IDCTDH,ctdh.IDSanPham,ctdh.IDDonHang,ctdh.SoLuong,ctdh.ThanhTien
	From #Temp as t Join DonHang as ctdh on t.IDCTDH = ctdh.IDCTDH
	Where  
		(@PageIndex = 0)
		Or
		t.IDTemp > (@PageIndex - 1) * @PageSize and t.IDTemp <= @PageIndex * @PageSize
	Drop table #Temp
----------------------------------


create procedure dbo.tbDiaDiem_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDDiaDiem int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDDiaDiem]) Select [IDDiaDiem] From DiaDiem ' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select dd.IDDiaDiem,dd.DiaDiem
	From #Temp as T Join DiaDiem as dd On T.IDDiaDiem = dd.IDDiaDiem
	Where 
		(@PageIndex = 0)
		Or
		(T.IDTemp > (@PageIndex - 1)*@PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
------------------------------

create procedure dbo.tbDoanhNghiep_Get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as
set transaction isolation level read committed
create table #Temp(
	IDTemp int identity(1,1),
	IDDoanhNghiep int
)
declare @sql nvarchar (1000)
set @sql = 'insert into #temp ([IDDN]) selete [IDDN] from DoanhNghiep ' + @Where + 'ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
select @ToTalRecords = COUNT(*) From #Temp
select 
	dn.IDDoanhNghiep,
	dn.TenDoanhNghiep,
	dn.DiaChi,
	dn.DienThoai,
	dn.Email
from #Temp as T join DoanhNghiep as S on T.IDDoanhNghiep = S.IDDoanhNghiep
where
	(@PageIndex = 0 )
	or
	(T.IDTemp > (@PageIndex - 1 ) * @PageSize and T.IDTemp <= @PageIndex * @PageSize)
drop table #temp
-----------------------------------

create Procedure dbo.tbDonHang_Get
	@Where NVarchar(255),
	@PageIndex int,
	@PageSize int,
	@OrderBy NVarchar(25),
	@OrderDirection NVarchar(4),
	@TotalRecords int output	
As
	set transaction isolation level read committed
	Create Table #Temp
	(
		IDTemp int identity(1,1),
		IDDonHang int
	)
	Declare @sql NVarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDDonHang]) Select [IDDonHang] From DonHang ' + @Where + ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec (@sql)
	Select @TotalRecords = count (*) from #Temp
	Select dh.IDDonHang,dh.IDKhachHang,dh.NgayDH,dh.IDTTDonHang,dh.TriGia,dh.NgayGiaoHang
	From #Temp as t Join DonHang as dh on t.IDDonHang = dh.IDDonHang
	Where  
		(@PageIndex = 0)
		Or
		t.IDTemp > (@PageIndex - 1) * @PageSize and t.IDTemp <= @PageIndex * @PageSize
	Drop table #Temp
----------------------------------

create procedure dbo.tbHinhAnh_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDHinhAnh int
	)	
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDHinhAnh] Select [IDHinhAnh] From HinhAnh) '	+ @Where
		+ 'Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select 
		ha.IDHinhAnh,
		ha.TenHinh,
		ha.IDSanPham
	From #Temp as T Join HinhAnh as ha on T.IDHinhAnh = ha.IDHinhAnh
	Where
		(@PageIndex = 0)
		Or
		(T.IDTemp > (@PageIndex - 1) * @PageSize And
		T.IDTemp <= @pageIndex * @PageSize) 
Drop table #Temp
-----------------------------------

create procedure dbo.tbHTThanhToan_Get
	@Where Nvarchar(250),	
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(4),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDHinhThuc int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDHinhThuc]) Select [IDHinhThuc] From HTThanhToan ' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select 
		tt.IDHinhThuc,
		tt.TenHinhThuc
	From #Temp as T Join HTThanhToan as tt On T.IDHinhThuc = tt.IDHinhThuc
	Where
		(@PageIndex = 0)
		Or
		(
			(T.IDTemp > (@PageIndex - 1) * @PageSize And T.IDTemp <= @PageIndex * @PageSize)
		)
	Drop table #Temp
------------------------------

create procedure dbo.tbKhachHang_Get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as
set transaction isolation level read committed
create table #Temp(
	IDTemp int identity(1,1),
	IDKhachHang int
)
declare @sql nvarchar (1000)
set @sql = 'insert into #temp ([IDKhachHang]) selete [IDKhachHang] from KhachHang ' + @Where + 'ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
select @ToTalRecords = COUNT(*) From #Temp
select 
	kh.IDKhachHang,
	kh.TenKH,
	kh.GioiTinh,
	kh.DiaChi,
	kh.DienThoai,
	kh.Email
from #Temp as T join KhachHang as S on T.IDKhachHang = S.IDKhachHang
where
	(@PageIndex = 0 )
	or
	(T.IDTemp > (@PageIndex - 1 ) * @PageSize and T.IDTemp <= @PageIndex * @PageSize)
drop table #temp
---------------------------

create procedure dbo.tbLoaiSP_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDLoaiSP int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDLoaiSP]) Select [IDLoaiSP] From LoaiSP ' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select lsp.IDLoaiSP,lsp.TenLoaiSP
	From #Temp as T Join LoaiSP as lsp On T.IDLoaiSP = lsp.IDLoaiSP
	Where
		(@PageIndex = 0)
		Or
			(T.IDTemp > (@PageIndex - 1)* @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
-------------------------
create procedure dbo.tbMaSoSP_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDMSSP int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDMSSP] Select [IDMSSP] From MaSoSP )' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select maso_sp.IDMSSP,maso_sp.IDCTDH,maso_sp.MaSo,maso_sp.TrangThai
	From #Temp as T Join MaSoSP as maso_sp On T.IDMSSP = maso_sp.IDMSSP
	Where
		(@PageIndex = 0)
		Or 
			(T.IDTemp > (@PageIndex - 1) * @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
---------------------
create procedure dbo.tbNhanVien_Get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as
set transaction isolation level read committed
create table #Temp(
	IDTemp int identity(1,1),
	IDNhanVien int
)
declare @sql nvarchar (1000)
set @sql = 'insert into #temp ([IDNhanVien]) selete [IDNhanVien] from NhanVien ' + @Where + 'ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
select @ToTalRecords = COUNT(*) From #Temp
select 
	S.IDNhanVien,
	S.TenNV,
	S.DiaChi,
	S.DienThoai,
	S.Email
from #Temp as T join NhanVien as S on T.IDNhanVien = S.IDNhanVien
where
	(@PageIndex = 0 )
	or
	(T.IDTemp > (@PageIndex - 1 ) * @PageSize and T.IDTemp <= @PageIndex * @PageSize)
drop table #Temp
---------------------
create procedure dbo.tbSanPham_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDSanPham int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDSanPham]) Select [IDSanPham] From SanPham )' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select 
		sp.IDSanPham,sp.TenSanPham,sp.IDLoaiSP,sp.IDDiaDiem,sp.IDHangSanXuat,sp.GiaBan,sp.GiaTriThuc,sp.DieuKienSuDung,sp.MoTaSP,
		sp.SLBaSoLuongSP,sp.SoLuongBan,sp.SoLuongDaBan,sp.IDTrangThaiSP,sp.NgayBDBan,sp.NgayKTBan,sp.NgayCapNhat,
		sp.IDNhanVien,sp.IDDoanhNghiep,sp.TiLeHoaHong
	From #Temp as T Join SanPham as sp On T.IDSanPham = sp.IDSanPham
	Where
		(@PageIndex = 0)
		Or 
			(T.IDTemp > (@PageIndex - 1) * @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
-----------------------

create procedure dbo.tbTrangThaiDH_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(4),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDTTDonHang int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDTTDonHang]) Select [IDTTDonHang] From TTDonHang ' + @Where + ' Order By ' +
	@OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select tt.IDTTDonHang,tt.TrangThaiDH
	From #Temp as T Join TTDonHang as tt On T.IDTTDonHang = tt.IDTTDonHang
	Where
		(@PageIndex = 0)
		Or
		(T.IDTemp > (@PageIndex - 1) * @PageSize and 
		T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
--------------------

create procedure dbo.tbTrangThaiSP_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDTrangThaiSP int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDTrangThaiSP]) Select [IDTrangThaiSP] From TrangThaiSP ' + @Where
	+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select tt_sp.IDTrangThaiSP, tt_sp.TrangThai
	From #Temp as T Join TrangThaiSP as tt_sp On T.IDTrangThaiSP = tt_sp.IDTrangThaiSP
	Where 
		(@PageIndex = 0)
		Or
		(T.IDTemp > (@PageIndex - 1)* @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
----------------
create procedure dbo.tbTTGiaoDich_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDTTGiaoDich int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDTTGiaoDich]) Select [IDTTGiaoDich] From TTGiaoDich ' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select 
		tt_gd.IDTTGiaoDich,tt_gd.IDDonHang,tt_gd.IDHinhThuc,tt_gd.ThanhTien
	From #Temp as T Join TTGiaoDich as tt_gd On T.IDTTGiaoDich = tt_gd.IDTTGiaoDich
	Where 
		(@PageIndex = 0)
		Or
			(T.IDTemp > (@PageIndex - 1) * @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp
-------------------------
create procedure dbo.tbHangSanXuat_Get
	@Where Nvarchar(250),
	@PageIndex int,
	@PageSize int,
	@OrderBy Nvarchar(250),
	@OrderDirection Nvarchar(250),
	@TotalRecords int output
As
	set transaction isolation level read committed
	create table #Temp
	(
		IDTemp int identity(1,1),
		IDHangSanXuat int
	)
	Declare @sql Nvarchar(1000)
	Set @sql = 'Insert Into #Temp ([IDHangSanXuat]) Select [IDHangSanXuat] From HangSanXuat ' + @Where
		+ ' Order By ' + @OrderBy + ' ' + @OrderDirection
	Exec(@sql)
	Select @TotalRecords = count(*) from #Temp
	Select 
		tt_gd.IDHangSanXuat,tt_gd.TenHangSanxuat,tt_gd.DiaChi,tt_gd.DienThoai,tt_gd.Email
	From #Temp as T Join HangSanXuat as tt_gd On T.IDHangSanXuat = tt_gd.IDHangSanXuat
	Where 
		(@PageIndex = 0)
		Or
			(T.IDTemp > (@PageIndex - 1) * @PageSize And T.IDTemp <= @PageIndex * @PageSize)
	Drop table #Temp